package com.alex.statistics.controller;

import com.alex.statistics.method.grouping.GroupingAnalyzer;
import com.alex.statistics.pojo.*;
import com.alex.statistics.pojo.request.groupanalysis.CustomRuleRequest;
import com.alex.statistics.pojo.request.groupanalysis.EqualIntervalRequest;
import com.alex.statistics.pojo.request.groupanalysis.NaturalBreaksRequest;
import com.alex.statistics.pojo.request.groupanalysis.QuantileRequest;
import com.alex.statistics.pojo.result.groupanalysis.GroupingAnalysisResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "Grouping Analysis API", description = "数据分组分析接口")
@RequestMapping("/grouping")
@RestController
public class GroupingAnalysisController {

    private final GroupingAnalyzer groupingAnalyzer;

    @Autowired
    public GroupingAnalysisController(GroupingAnalyzer groupingAnalyzer) {
        this.groupingAnalyzer = groupingAnalyzer;
    }

    @Operation(summary = "分位数法分组", description = "基于数据分布的分位数划分组别")
    @PostMapping("/quantile")
    public Result<GroupingAnalysisResult> quantileGrouping(
            @RequestBody QuantileRequest request) {
        double[] quantiles = request.getQuantiles().stream()
                .mapToDouble(Double::doubleValue)
                .toArray();
        List<Double> boundaries = groupingAnalyzer.quantileGrouping(
                request.getData(), quantiles);
        return createResult(boundaries, request.getData());
    }

    @Operation(summary = "自然断点法分组",
            description = "通过组间差异最大、组内差异最小的分组边界划分数据组别")
    @PostMapping("/natural-breaks")
    public Result<GroupingAnalysisResult> naturalBreaksGrouping(
            @RequestBody NaturalBreaksRequest request) {
        List<Double> boundaries = groupingAnalyzer.naturalBreaksGrouping(
                request.getData(), request.getGroupCount());
        return createResult(boundaries, request.getData());
    }

    @Operation(summary = "等间距法分组",
            description = "通过固定间隔对数据进行均匀分组")
    @PostMapping("/equal-interval")
    public Result<GroupingAnalysisResult> equalIntervalGrouping(
            @RequestBody EqualIntervalRequest request) {
        List<Double> boundaries = groupingAnalyzer.equalIntervalGrouping(
                request.getData(), request.getInterval());
        return createResult(boundaries, request.getData());
    }

    @Operation(summary = "自定义规则法分组",
            description = "根据业务需求通过自定义规则划分数据组别")
    @PostMapping("/custom-rule")
    public Result<GroupingAnalysisResult> customRuleGrouping(
            @RequestBody CustomRuleRequest request) {
        List<Double> boundaries = groupingAnalyzer.customRuleGrouping(
                request.getData(), request.getBoundaries());
        return createResult(boundaries, request.getData());
    }

    private Result<GroupingAnalysisResult> createResult(
            List<Double> boundaries, List<Double> data) {
        GroupingAnalysisResult result = new GroupingAnalysisResult();
        result.setBoundaries(boundaries);
        result.setGroupStats(
                groupingAnalyzer.calculateGroupStats(data, boundaries));
        return Result.ok(result);
    }
}